package com.bee.portal.service.sm.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;

import com.bee.base.entity.Page;
import com.bee.portal.dao.sm.RoleDao;
import com.bee.portal.domain.sm.Role;
import com.bee.portal.service.sm.RoleService;

public class RoleServiceImpl implements RoleService {

	private RoleDao roleDao;
	private JdbcTemplate jdbcTemplate;

	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Override
	public Serializable save(Role entity) {
		return roleDao.save(entity);
	}

	@Override
	public void saveAll(Collection<Role> entities) {
		roleDao.saveAll(entities);
	}

	@Override
	public Role get(Long sid) {
		return roleDao.get(sid);
	}

	@Override
	public List<Role> find(String queryString, Object... values) {
		return roleDao.find(queryString, values);
	}

	@Override
	public List<Role> find(Role entity) {
		return roleDao.find(entity);
	}

	@Override
	public List<Role> find(Map<String, String> params) {
		return roleDao.find(params);
	}

	@Override
	public List<Role> findAll() {
		return roleDao.findAll();
	}

	@Override
	public void update(Role entity) {
		roleDao.update(entity);
	}

	@Override
	public void updateAll(Collection<Role> entities) {
		roleDao.updateAll(entities);
	}

	@Override
	public void delete(Role entity) {
		roleDao.delete(entity);
	}

	@Override
	public void delete(Long sid) {
		roleDao.delete(sid);
	}

	@Override
	public void deleteAll(Collection<Role> entities) {
		roleDao.deleteAll(entities);
	}

	@Override
	public Page page(String queryString, int pageNo, int pageSize) {
		return roleDao.page(queryString, pageNo, pageSize);
	}

	@Override
	public Page page(Map<String, String> params, int pageNo, int pageSize) {
		return roleDao.page(params, pageNo, pageSize);
	}

	@Override
	public void saveRoleAndResource(Long sid, List<Long> ids) {
		StringBuffer sql1 = new StringBuffer();
		sql1.append("DELETE ");
		sql1.append("FROM ");
		sql1.append("SM_ROLE_RESOURCE ");
		sql1.append("WHERE ");
		sql1.append("SM_ROLE_SID = " + sid + " ");
		jdbcTemplate.execute(sql1.toString());
		if (ids.size() > 0) {
			StringBuffer sql2 = new StringBuffer();
			sql2.append("INSERT INTO ");
			sql2.append("SM_ROLE_RESOURCE ");
			sql2.append("SELECT ");
			sql2.append("NULL, a.SM_ORGANIZATION_SID, a.SID, b.SID ");
			sql2.append("FROM ");
			sql2.append("SM_ROLE a, SM_RESOURCE b ");
			sql2.append("WHERE ");
			sql2.append("a.SID = " + sid + " ");
			sql2.append("AND ");
			sql2.append("b.SID IN(" + ids.toString().replace("[", "").replace("]", "") + ") ");
			jdbcTemplate.execute(sql2.toString());
		}
	}

}
